package com.hz;

import com.hz.SM2Utils.SM2KeyDerive;
import com.hz.SM2Utils.SM2Util;
import org.bouncycastle.util.encoders.Hex;
import org.bouncycastle.util.encoders.HexEncoder;

import java.security.SecureRandom;

public class Main {
    public static void main(String[] args) {
        byte[][] SM2KeyPair=SM2Util.SM2_GenerateKeyPair();
        byte[] SM2publicKey=SM2KeyPair[0];
        byte[] SM2privateKey=SM2KeyPair[1];

        //TEST Sign and verify
        String message="hello SM2";
        byte[] SM2Sign=SM2Util.SM2_Sign(SM2privateKey,message.getBytes());

        boolean flag=SM2Util.SM2_Verifysign(SM2publicKey,message.getBytes(),SM2Sign);
        if(flag){
            System.out.println("verify pass");
        }

        //TEST SM2KeyDerive
        int Keyindex=0;
        SecureRandom secureRandom = new SecureRandom();
        byte[] chain = new byte[32];
        secureRandom.nextBytes(chain);

        byte[][] pkandchian= SM2KeyDerive.SM2_PublicKeyDerive(SM2publicKey,Keyindex,chain);
        byte[]newSM2PK=pkandchian[0];
        byte[][] skandchain=SM2KeyDerive.SM2_PrivateKeyDerive(SM2privateKey,Keyindex,chain);
        byte[]newSM2SK=skandchain[0];

        byte[] newSM2Sign=SM2Util.SM2_Sign(newSM2SK,message.getBytes());

        boolean flag2=SM2Util.SM2_Verifysign(newSM2PK,message.getBytes(),newSM2Sign);
        if(flag2){
            System.out.println("verify pass,SM2KeyDrive is OK");
        }











    }
}